//
//  ShortVideoViewController.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/2/27.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "ShortVideoViewController.h"
#import "HomeVideoListViewController.h"
#import "VideoRecordViewController.h"
#import "CYImagePickerViewController.h"
#import "VideoEditViewController.h"
#import "MineLogic.h"

@interface ShortVideoViewController ()<TZImagePickerControllerDelegate,MineDelegate>
{
    NSArray *list;
    MineLogic *_logic;
}

@property (nonatomic, strong) MLMSegmentHead *segHead;
@property (nonatomic, strong) MLMSegmentScroll *segScroll;
@property(nonatomic, strong) UIButton *cameraButton;
@property(nonatomic, strong) QMUIPopupMenuView *popupView2;

@end

@implementation ShortVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self segmentStyle1];
    self.isShowLiftBack = NO;//每个根视图需要设置该属性为NO，否则会出现导航栏异常
    
    _cameraButton = [[UIButton alloc] init];
    [_cameraButton setImage:IMAGE_NAMED(@"sd_camera") forState:UIControlStateNormal];
//    _cameraButton.frame = CGRectMake(KScreenWidth - 40, 22, 35, 35);
    _cameraButton.frame = CGRectMake(KScreenWidth - 40, StatusBarHeight, 35, 35);
    [self.view addSubview:_cameraButton];
    __weak __typeof(self)weakSelf = self;
    // 使用方法 2，以 UIWindow 的形式显示到界面上，这种无需默认隐藏，也无需 add 到某个 UIView 上
    self.popupView2 = [[QMUIPopupMenuView alloc] init];
    self.popupView2.automaticallyHidesWhenUserTap = YES;// 点击空白地方消失浮层
    self.popupView2.maskViewBackgroundColor = UIColorMaskWhite;// 使用方法 2 并且打开了 automaticallyHidesWhenUserTap 的情况下，可以修改背景遮罩的颜色
    self.popupView2.maximumWidth = 180;
    self.popupView2.shouldShowItemSeparator = YES;
    self.popupView2.separatorInset = UIEdgeInsetsMake(0, self.popupView2.padding.left, 0, self.popupView2.padding.right);
    self.popupView2.items = @[
      [QMUIPopupMenuItem itemWithImage:[UIImageMake(@"sd_shootVideo") imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] title:ASLocalizedString(@"拍摄视频")handler:^{
          dispatch_async(dispatch_get_main_queue(), ^{
             [self jumpshootVideo];
          });
          [weakSelf.popupView2 hideWithAnimated:YES];
      }],
      [QMUIPopupMenuItem itemWithImage:[UIImageMake(@"sd_uploadVideo") imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] title:ASLocalizedString(@"上传视频")handler:^{
          
          dispatch_async(dispatch_get_main_queue(), ^{
              [self selectVideo];
          });
          
          [weakSelf.popupView2 hideWithAnimated:YES];
      }]];
    
    self.popupView2.didHideBlock = ^(BOOL hidesByUserTap) {

    };
    [self.popupView2 layoutWithTargetView:self.cameraButton];// 相对于 button2 布局
    [self.popupView2 layoutWithTargetRectInScreenCoordinate:CGRectMake(self.cameraButton.centerX, -50,100, 100)];

    [self.cameraButton addTarget:self action:@selector(handleShowPOPEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    _logic = [[MineLogic alloc] init];
    _logic.delegagte = self;
    [_logic loadData];
}

//跳转到录制视频
-(void)jumpshootVideo
{
    VideoConfigure* _videoConfig = [[VideoConfigure alloc] init];
    
    VideoRecordViewController *vc = [[VideoRecordViewController alloc] initWithConfigure:_videoConfig];
    [self.navigationController pushViewController:vc animated:YES];
//    [APP_DELEGATE pushViewController:vc];
}


-(void)handleShowPOPEvent:(UIButton *)button
{
    if(_logic.model.user_auth_status == 1)
    {
        [self.popupView2 showWithAnimated:YES];
    }
    else
    {
        [[HUDHelper sharedInstance] tipMessage:ASLocalizedString(@"认证通过后才能上传视频哦")];
    }
    
//    [self.popupView2 showWithAnimated:YES];
}
- (void)segmentStyle1 {
    list = @[ASLocalizedString(@"推荐"),
             ASLocalizedString(@"最新"),
             ASLocalizedString(@"关注"),
             //             ASLocalizedString(@"同城"),
             ASLocalizedString(@"附近")];
    _segHead = [[MLMSegmentHead alloc] initWithFrame:CGRectMake(0, StatusBarHeight, SCREEN_WIDTH/4*2.8, 40) titles:list headStyle:SegmentHeadStyleLine layoutStyle:_layout];
    _segHead.centerX = self.view.centerX;
    //tab颜色
    _segHead.selectColor = MainColor;
    _segHead.deSelectColor = [UIColor blackColor];
    _segHead.lineColor = MainColor;
    _segHead.fontScale = 1;
    _segHead.lineHeight = 1.3;
    _segHead.fontSize = 15;
    _segHead.lineScale = .9;
    _segHead.headColor = CF6;
    _segHead.bottomLineHeight = 0;
    self.view.backgroundColor = CF6;
    _segScroll = [[MLMSegmentScroll alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_segHead.frame), SCREEN_WIDTH, SCREEN_HEIGHT-CGRectGetMaxY(_segHead.frame)) vcOrViews:[self vcArr:list.count]];
    _segScroll.loadAll = NO;
    _segScroll.showIndex = 0;
    
    [MLMSegmentManager associateHead:_segHead withScroll:_segScroll completion:^{
        [self.view addSubview:_segHead];
        [self.view addSubview:_segScroll];
    }];
}

#pragma mark - 数据源
- (NSArray *)vcArr:(NSInteger)count {
    NSMutableArray *arr = [NSMutableArray array];

    //视频推荐
    HomeVideoListViewController *order1 = [HomeVideoListViewController new];
    order1.type = @"reference";
    [arr addObject:order1];

    //视频最新
    HomeVideoListViewController *order2 = [HomeVideoListViewController new];
    order2.type = @"latest";
    [arr addObject:order2];

    //视频关注
    HomeVideoListViewController *order3 = [HomeVideoListViewController new];
    order3.type = @"attention";
    [arr addObject:order3];
    
    //新人
    HomeVideoListViewController *order4 = [HomeVideoListViewController new];
//    order4.type = @"3";
    order4.type = @"reference";
    [arr addObject:order4];
    
    return arr;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //隐藏导航栏
    self.isHidenNaviBar = YES;
    self.navigationController.navigationBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)selectVideo
{
    CYImagePickerViewController *imagePickerVc = [[CYImagePickerViewController alloc] initWithMaxImagesCount:1 delegate:self];
    [imagePickerVc.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    imagePickerVc.naviBgColor = [UIColor blackColor];
    // You can get the photos by block, the same as by delegate.
    // 你可以通过block或者代理，来得到用户选择的照片.
    
    
//    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
//        TZImagePickerController *imagePicker = [[TZImagePickerController alloc] initCropTypeWithAsset:assets[0] photo:photos[0] completion:^(UIImage *cropImage, id asset) {
//            _headerImage = cropImage;
//            [_loginHeaderImage setImage:_headerImage forState:UIControlStateNormal];
//        }];
//        imagePicker.needCircleCrop = YES;
//        imagePicker.circleCropRadius = 110;
//        [self presentViewController:imagePicker animated:YES completion:nil];
//
//    }];
    
    __weak __typeof(self) weakSelf = self;

    [imagePickerVc setDidFinishPickingVideoHandle:^(UIImage *coverImage, id asset) {
        if(![asset isKindOfClass:[PHAsset class]])
        {
            [[HUDHelper sharedInstance] tipMessage:ASLocalizedString(@"手机系统版本较低!")];
            return;
        }

        [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset * avasset, AVAudioMix * audioMix, NSDictionary * info) {
            dispatch_async(dispatch_get_main_queue(), ^{
                AVURLAsset *urlAsset = (AVURLAsset *)avasset;

                VideoEditViewController *editVideo = [[VideoEditViewController alloc] init];
//                editVideo.videoAsset = avasset;
                [editVideo setVideoPath:[urlAsset.URL absoluteString]];
//                editVideo.videoAsset = avasset;
                [kAppDelegate pushViewController:editVideo];
                
//                [weakSelf doSomethingWithAsset:avasset];
            });
        }];

    }];
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.allowPickingGif = NO;
    imagePickerVc.allowPickingImage = NO;
    imagePickerVc.naviTitleColor = [UIColor blackColor];
    imagePickerVc.barItemTextColor =  [UIColor blackColor];
    imagePickerVc.oKButtonTitleColorNormal = MainColor;
    imagePickerVc.showSelectBtn = YES;
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
